import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'MainScreen',
      component: require('@/views/MainScreen').default,
      children: [
        {
          path: '',
          name: 'SettleIndex',
          component: require('@/components/settle/index').default,
          meta: {keepAlive: true}
        },
        // 便利店
        {
          path: 'store',
          name: 'StoreIndex',
          component: require('@/views/store/index').default,
          children: [
            {
              path: '',
              name: 'StoreSale',
              component: require('@/views/store/components/SaleList').default,
              meta: {keepAlive: false}
            },
            {
              path: 'stock',
              name: 'StoreStock',
              component: require('@/views/store/components/StockList').default,
              meta: {keepAlive: false}
            },
            {
              path: 'promotion',
              name: 'StorePromotion',
              component: require('@/views/store/components/PromotionList').default,
              meta: {keepAlive: false}
            }
          ]
        },
        // 储值卡
        {
          path: 'card',
          name: 'CardIndex',
          component: require('@/views/card/index').default,
          children: [
            {
              path: '',
              name: 'CardMenuPanel',
              component: require('@/views/card/CardMenuPanel').default,
              meta: {keepAlive: false}
            },
            // 卡查询
            {
              path: 'info',
              name: 'CardInfo',
              component: require('@/views/card/CardInfo').default,
              meta: {keepAlive: false}
            },
            // 主卡创建
            {
              path: 'master',
              name: 'AddMasterCard',
              component: require('@/views/card/AddMasterCard').default,
              meta: {keepAlive: false}
            },
            // 副卡创建
            {
              path: 'second',
              name: 'AddSecondCard',
              component: require('@/views/card/AddSecondCard').default,
              meta: {keepAlive: false}
            },
            // 卡充值
            {
              path: 'recharge',
              name: 'CardRecharge',
              component: require('@/views/card/CardRecharge').default,
              meta: {keepAlive: false}
            },
            // 卡挂失解挂失
            {
              path: 'lock',
              name: 'CardLock',
              component: require('@/views/card/CardLock').default,
              meta: {keepAlive: false}
            },
            // 换卡
            // 会员绑定/解绑
            {
              path: 'bind',
              name: 'CardMemberBind',
              component: require('@/views/card/CardMemberBind').default,
              meta: {keepAlive: false}
            },
            {
              path: 'init',
              name: 'CardInit',
              component: require('@/views/card/CardInit').default,
              meta: {keepAlive: false}
            }
          ]
        },
        // 历史记录
        {
          path: 'history',
          name: 'HistoryIndex',
          component: require('@/views/history/index').default,
          children: [
            {
              path: '',
              name: 'HistoryMenuPanel',
              component: require('@/views/history/HistoryMenuPanel').default,
              meta: {keepAlive: false}
            },
            {
              path: 'fuel',
              name: 'SaleFuelRecords',
              component: require('@/views/history/SaleFuelRecords').default,
              meta: {keepAlive: false}
            },
            {
              path: 'commodity',
              name: 'SaleCommodityRecords',
              component: require('@/views/history/SaleCommodityRecords').default,
              meta: {keepAlive: false}
            },
            {
              path: 'recharge',
              name: 'CardRechargeRecords',
              component: require('@/views/history/CardRechargeRecords').default,
              meta: {keepAlive: false}
            }
          ]
        }
      ]
    },
    {
      path: '/secondary',
      name: 'SecondaryScreen',
      component: require('@/views/SecondaryScreen').default
    },
    {
      path: '/login',
      name: 'Login',
      component: require('@/views/Login').default
    }
  ]
})

router.beforeEach((to, from, next) => {
  // ...
  // 登陆界面和副屏界面不需要登陆
  if (!store.getters.hasLogin && to.path !== '/login' && to.path !== '/secondary') {
    // next('login')
    next()
  } else {
    next()
  }
})

export default router
